<?php

namespace App\Http\Controllers\meizu;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
class storeController extends Controller
{
    //魅族商城
    public function index()
    {
    	// 上导航栏1表示
    	$top = 1;
    	$zoneT = DB::table('zone') ->where('place',$top) ->  get();
    	// 获取上导航栏商品
    	foreach($zoneT as $k => $v)
    	{
    		$goodsT[$k] = DB::table('goods') 
    					-> where('pid',$v -> pid) 
    					-> where('new','<>',1) 
    					-> Where('key',$v -> key)
    					-> get();
    	}

    	//左导航栏
    	$left = 0;
    	$zoneL = DB::table('zone') ->where('place',$left) ->  get();
    	$titleL = DB::table('zone') ->where('place',-1) ->  first();

    	// 获取左导航栏商品
    	foreach($zoneL as $k => $v)
    	{
    		$goodsL[$k] = DB::table('goods') 
    					-> where('pid',$v -> id) 
    					-> where('new','<>',1)
    					-> orderBy('id','desc')
    					-> limit(10)
    					-> get();
    		//左导航栏新品热品
    		$newL[$k]  = DB::table('goods') 
    					-> where('pid',$v -> id) 
    					-> where('new','=',1)
    					-> orderBy('id','desc')
    					-> first();
    	}
    	
    	//ad 高端广告区
    	$adnum = 0;
    	$ad = DB::table('goods') ->where('pid',$adnum) ->  get();
    	//广告区下bottom
    	$adB = -1;
    	$adB = DB::table('goods') ->where('pid',$adB) ->  get();
    	
    	//下边模块
    	$down = 2;
    	$zoneD = DB::table('zone') ->where('place',$down) ->  get();
    	// 下边版块的小版块
    	foreach($zoneD as $k => $v)
    	{
    		$zoneDs[$k] = DB::table('zone') 
    					-> where('pid',$v -> id) 
    					-> Where('place',3)
    					-> get();
    		//下边版块的精品
    		$goodsD[$k] = DB::table('goods') 
    					-> where('pid',$v -> id) 
    					-> Where('fire',1)
    					-> first();
    		//下边版块内容
    		$goodsMess[$k] = DB::table('goods') 
    					-> where('path',$v -> pid .'-'.$v -> id) 
    					-> get();
    	}


    	//热品，新品区
    	$hot = DB::table('goods') ->where('hot','=',1) -> limit(12) -> get();
    	// 拦腰
    	$sign = DB::table('goods') ->where('pid','=',-2) -> get();

<<<<<<< HEAD

        if((session() -> has('user'))&&( !empty(session('user'))))
        {
             
            $id = session('user');
            $data = DB::table('user') -> where('id',$id)->first();
            $detail = DB::table('userdetail') -> where('id',$id) ->first();

       
        }else{
            $data = null;
            $detail = null;
        }
    	// dd($data);
=======
    	// dd($goodsMess);
>>>>>>> 4e7aece960fd81ef164ce272747553aacde173f3
    	return view('meizu.index',
    		['zoneT' => $zoneT,
    		'goodsT' => $goodsT,
    		'zoneL'  => $zoneL,
    		'titleL' => $titleL,
    		'goodsL' => $goodsL,
    		'newL'   => $newL,
    		'ad'     => $ad,
    		'adB'    => $adB,
    		'hot'	 => $hot,
    		'zoneD'  => $zoneD,
    		'sign'   => $sign,
    		'zoneDs' => $zoneDs,
    		'goodsD' => $goodsD,
    		'goodsMess' => $goodsMess,
<<<<<<< HEAD
            //用户信息
            'data'   => $data,
            'detail'   => $detail,
    		]);
    }
    public function goodslist($id)
=======
    		]);
    }
    public function goodslist()
>>>>>>> 4e7aece960fd81ef164ce272747553aacde173f3
    {
    	// 上导航栏1表示
    	$top = 1;
    	$zoneT = DB::table('zone') ->where('place',$top) ->  get();
    	// 获取上导航栏商品
    	foreach($zoneT as $k => $v)
    	{
    		$goodsT[$k] = DB::table('goods') 
    					-> where('pid',$v -> pid) 
    					-> where('new','<>',1) 
    					-> Where('key',$v -> key)
    					-> get();
    	}
<<<<<<< HEAD
    	//左导航栏
    	$left = 0;
    	$zoneL = DB::table('zone') ->where('place',$left) ->  get();
        $titleL = DB::table('zone') ->where('place',-1) ->  first();
    	$titleT = DB::table('zone') ->where('id',$id) ->  first();

    	// 获取左导航栏商品
    	foreach($zoneL as $k => $v)
    	{
    		$goodsL[$k] = DB::table('goods') 
    					-> where('pid',$v -> id) 
    					-> where('new','<>',1)
    					-> orderBy('id','desc')
    					-> limit(10)
    					-> get();
    	}
        // 列表页
        // 如果有搜索条件
        if((session() -> has('search'))&&( !empty(session('search'))))
        {
             $list  = DB::table('goods') 
                        -> where('name','like','%'.session('search').'%')
                        -> Orwhere('type','like','%'.session('search').'%')
                        -> where('new','<>',1)
                        -> paginate(12);
            session(['search' => '']);
        }
        elseif($id == $titleL -> id)
        {
            $list  = DB::table('goods') 
                        -> where('pid','>',0) 
                        -> where('pid','<',8) 
                        -> where('new','<>',1)
                        -> orderBy('id','desc')
                        -> paginate(12);
;
        }else{

             $list  = DB::table('goods') 
                        -> where('pid','=',$id) 
                        -> where('new','<>',1)
                        -> orderBy('id','desc')
                        -> paginate(12);
                        // -> get();
;
        }

    	//品牌 place 是4
    	$brand = 4;
    	$zoneBrand = DB::table('zone') ->where('place',$brand) ->  get();

    	//热品，新品区
    	$hot = DB::table('goods') ->where('hot','=',1) -> limit(12) -> get();
    	

         if((session() -> has('user'))&&( !empty(session('user'))))
        {
             
            $id = session('user');
            $data = DB::table('user') -> where('id',$id)->first();
            $detail = DB::table('userdetail') -> where('id',$id) ->first();

       
        }else{
            $data = null;
            $detail = null;
        }
    	// dd($order);
    	return view('meizu.list',
    		['zoneT' => $zoneT,
    		'goodsT' => $goodsT,
    		'zoneL'  => $zoneL,
            'titleL' => $titleL,
    		'titleT' => $titleT,
    		'goodsL' => $goodsL,
    		'zoneBrand' => $zoneBrand,
    		'hot'    => $hot,
            'list'   => $list,
            //用户信息
            'data'   => $data,
            'detail'   => $detail,
=======
    	// dd($goodsMess);
    	return view('meizu.list',
    		['zoneT' => $zoneT,
    		'goodsT' => $goodsT,
>>>>>>> 4e7aece960fd81ef164ce272747553aacde173f3
    		]);
    }
    public function search(Request $request)
    {
        $search = $request  -> except('_token');
        session(['search' => $search['search']]);
        return back();
    }
    public function detail($id)
    {
        // 上导航栏1表示
        $top = 1;
        $zoneT = DB::table('zone') ->where('place',$top) ->  get();
        // 获取上导航栏商品
        foreach($zoneT as $k => $v)
        {
            $goodsT[$k] = DB::table('goods') 
                        -> where('pid',$v -> pid) 
                        -> where('new','<>',1) 
                        -> Where('key',$v -> key)
                        -> get();
        }
        //手机信息
        $titleT = DB::table('goods') ->where('id',$id) ->  first();

        // 处理数据
       $arr = explode('-',$titleT -> netweb);
       !empty($titleT -> netweb)?$titleT -> netweb = $arr:'';

     
       $arr = explode('-',$titleT -> color);
       !empty($titleT -> color)?$titleT -> color = $arr:'';

       $arr = explode('-',$titleT -> rom);
       !empty($titleT -> rom)?$titleT -> rom = $arr:'';

       $colorpic = explode('{-}',$titleT -> colorpic);
       
        if((session() -> has('user'))&&( !empty(session('user'))))
        {
             
            $id = session('user');
            $data = DB::table('user') -> where('id',$id)->first();
            $detail = DB::table('userdetail') -> where('id',$id) ->first();

       
        }else{
            $data = null;
            $detail = null;
        }
       // dd($titleT);
       return view('meizu.goods',
            ['zoneT'   => $zoneT,
            'goodsT'   => $goodsT,
            'titleT'   => $titleT,
            'colorpic' => $colorpic,
             //用户信息
            'data'   => $data,
            'detail'   => $detail,
            ]);
    }
    public function buy($id)
    {
         $top = 1;
        $zoneT = DB::table('zone') ->where('place',$top) ->  get();
        // 获取上导航栏商品
        foreach($zoneT as $k => $v)
        {
            $goodsT[$k] = DB::table('goods') 
                        -> where('pid',$v -> pid) 
                        -> where('new','<>',1) 
                        -> Where('key',$v -> key)
                        -> get();
        }
        //手机信息
        $titleT = DB::table('order') ->where('id',$id) ->  first();
        
         if((session() -> has('user'))&&( !empty(session('user'))))
        {
             
            $id = session('user');
            $data = DB::table('user') -> where('id',$id)->first();
            $detail = DB::table('userdetail') -> where('id',$id) ->first();

       
        }else{
            $data = null;
            $detail = null;
        }

        // dd($titleT);
       return view('meizu.buy',
            ['zoneT'   => $zoneT,
            'goodsT'   => $goodsT,
            'titleT'   => $titleT,
             //用户信息
            'data'   => $data,
            'detail'   => $detail,
            ]);
    }
    public function insert(Request $request,$id)
    {

        if((session() -> has('user'))&&( !empty(session('user'))))
        {

            $arr = $request -> except('_token');

            isset($arr['netweb'])?$arr['netweb'] .= ' ':$arr['netweb'] = '';
            isset($arr['color'])?$arr['color'] .= ' ':$arr['color'] = '';
            isset($arr['rom'])?$arr['rom'] .= ' ':$arr['rom'] = '';

            $order['info'] = $arr['netweb'].$arr['color'].$arr['rom'];
            $order['num'] = $arr['num'];

            $arr =  DB::table('goods') ->where('id',$id) ->  first();
            $order['name'] = $arr  -> type .' '.$arr -> name;
            $order['pic'] = $arr  -> pic;
            $order['price'] = $arr  -> price;

            $res = DB::table('order') -> insert($order);
            if($res)
            {
                $orderID = DB::table('order') -> orderBy('id','desc') -> select('id') ->first();
               
                   

                     return redirect("/buy/".$orderID -> id); 
                    
            }
        }else {

            return redirect('/login');
        }
        
    }
    public function userorder(Request $request ,$id)
    {
        $user = $request -> except('_token');
        if(empty($user['address']))
        {
            return back();
        }

        $uid = session('user');
        $order = DB::table('user') -> where('id',$uid) -> select('order','name') ->first();
        $phone = DB::table('userdetail') -> where('id',$uid) -> select('phone') ->first();
        
        if(empty($user['phone']))
        {
            $user['phone'] = $phone -> phone;
        }
        if(empty($user['username']))
        {
            $user['username'] = $order -> name;
        }
      
      
     
        $user['time'] = time();
        $user['uid'] = $uid;
       
        DB::table('order') -> where('id',$id) -> update($user); 
        return redirect('/user/order');
    }
}
